package com.wolfsea.rvitemdecorationdemo.rvadapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.AppCompatTextView
import androidx.recyclerview.widget.RecyclerView
import com.wolfsea.rvitemdecorationdemo.R
import com.wolfsea.rvitemdecorationdemo.bean.Contact
import com.wolfsea.rvitemdecorationdemo.databinding.DashboardRvItemBinding

/**
 *@desc  Dashboard适配器
 *@author liuliheng
 *@time 2022/3/19  23:31
 **/
class DashboardRvAdapter : RecyclerView.Adapter<DashboardRvAdapter.DashboardViewHolder>(){

    var items:List<Contact> = ArrayList()
        set(value) {
            field = value
            notifyDataSetChanged()
        }

    //private lateinit var binding: DashboardRvItemBinding

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DashboardViewHolder {
        //binding = DashboardRvItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.dashboard_rv_item, parent, false)
        return DashboardViewHolder(view)
    }

    override fun onBindViewHolder(holder: DashboardViewHolder, position: Int) {
        //binding.tvDashboardContent.text = items[position].name
        holder.bindData(items[position])
    }

    override fun getItemCount(): Int = items.size

    class DashboardViewHolder(view: View) : RecyclerView.ViewHolder(view) {

        private val tvDashContent = view.findViewById<AppCompatTextView>(R.id.tv_dashboard_content)

        fun bindData(contact: Contact) {

            tvDashContent.text = contact.name
        }
    }

}